package com.jhws.sys.cms.api;

import com.jhws.sys.cms.bo.CommGroup;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.cms.service.CmsRedisService;
import com.jhws.sys.cms.bo.RichText;
import com.jhws.sys.cms.service.AppResourceService;
import com.jhws.sys.cms.service.CommGroupService;
import com.jhws.sys.cms.service.RichTextService;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/30.
 */
@Controller
@RequestMapping("/v3/cms")
public class CmsApiV3 extends CmsApiV2 {
    @Resource
    private AppResourceService appResourceService;
    @Resource
    private CommGroupService commGroupService;
    @Resource
    private RichTextService richTextService;
    @Resource
    private CmsRedisService redisService;

    /**
     * 根据小区获取资源
     *
     * @param typeCode 资源code
     * @param commCode 小区编号
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping({"anon/queryResourcesByComm", "queryResourcesByComm"})
    @ResponseBody
    public JsonRespData queryResourcesByComm(String typeCode, String commCode) throws IOException {
        if (StringUtils.isEmpty(typeCode) || StringUtils.isEmpty(commCode)) {
            return new JsonRespData("queryResourcesByComm", -1, "参数不能为空！");
        }
        List<Map<String, Object>> ret = redisService.getRedisCMS(typeCode, commCode);
        if (ret == null) {
            List<CommGroup> groupList = commGroupService.findCommGroupListByCommCode(commCode);
            ret = appResourceService.queryResourceListApi(typeCode, groupList, commCode);
            redisService.addRedisCMS(typeCode, commCode, ret);
        }
        return new JsonRespData("queryResourcesByComm", 1, ret);
    }

    /**
     * 根据小区获取默认资源
     *
     * @param typeCode 资源code
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "anon/queryDefaultResources")
    @ResponseBody
    public JsonRespData queryDefaultResources(String typeCode) throws IOException {
        if (StringUtils.isEmpty(typeCode)) {
            return new JsonRespData("queryResourcesByComm", -1, "参数不能为空！");
        }
        List<Map<String, Object>> ret = redisService.getRedisCMS(typeCode, Global.DEFAULT_COMM);
        if (ret == null) {
            ret = appResourceService.queryResourceListApi(typeCode, null, Global.DEFAULT_COMM);
            redisService.addRedisCMS(typeCode, Global.DEFAULT_COMM, ret);
        }
        return new JsonRespData("queryResourcesByComm", 1, ret);
    }

    /**
     * 获取富文本页面
     * @param ueditorId  文本id号
     * @param template   模版名
     * @return
     */
    @RequestMapping(value = "anon/getContent")
    public ModelAndView getContent(String ueditorId, String template) {
        RichText ueEditor = richTextService.queryUeditorInfo(ueditorId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("title", ueEditor.getDescription());
        mv.addObject("content", ueEditor.getContent());
        if(StringUtils.isNotEmpty(template))
            mv.setViewName("business/ueditor/templates/"+template);
        else
            mv.setViewName("business/ueditor/templates/Uecontent");
        return mv;
    }
}
